<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/3/14
 * Time: 22:54
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Logic\MessageLogic;
use app\Module\Index\Logic\MessageType;
use app\Module\Index\Model\CirclePhotoCommentModel;
use app\Module\Index\Model\CirclePhotoReplyModel;
use app\Module\Index\Model\UnreadCirclePhotoReplyModel;

class CirclePhotoReplyController extends CheckAuth
{
    /**
     * @verify string $content
     * @verify int    $target_user_id
     * @verify int    $circle_photo_comment_id
     */
    public function addAction($content, $target_user_id, $circle_photo_comment_id)
    {
        if ($target_user_id == $this->userId) {
            return fail('不能回复自己');
        }
        $circlePhotoCommentModel = new CirclePhotoCommentModel();
        $row                     = $circlePhotoCommentModel->row(['id' => $circle_photo_comment_id]);
        if (empty($row) || $row['user_id'] != $target_user_id) {
            return fail('回复失败');
        }

        // 新增回复
        $circlePhotoReplyModel = new CirclePhotoReplyModel();
        $id                    = $circlePhotoReplyModel->insert([
            'content'                 => $content,
            'target_user_id'          => $target_user_id,
            'circle_photo_comment_id' => $circle_photo_comment_id,
            'user_id'                 => $this->userId,
            'created'                 => now(),
        ]);

        // 加入未读
        $unreadCirclePhotoReplyModel = new UnreadCirclePhotoReplyModel();
        $unreadCirclePhotoReplyModel->addSet([
            'updated'                    => now(),
            'last_circle_photo_reply_id' => $id,
        ]);
        $result = $unreadCirclePhotoReplyModel->update('num=num+1', [
            'user_id'         => session('userId'),
            'target_user_id'  => $target_user_id,
            'circle_photo_id' => $row['circle_photo_id'],
        ]);
        if (empty($result)) {
            $unreadCirclePhotoReplyModel->insert([
                'circle_photo_id'            => $row['circle_photo_id'],
                'target_user_id'             => $target_user_id,
                'last_circle_photo_reply_id' => $id,
                'user_id'                    => $this->userId,
                'created'                    => now(),
                'updated'                    => now(),
                'num'                        => 1,
            ]);
        }

        if ($id) {
            $messageLogic = new MessageLogic();
            $messageLogic->add($this->userId, $row['user_id'], $row['circle_photo_id'], MessageType::CIRCLE_PHOTO_REPLY, $row['content']);

            return success();
        } else {
            return fail('回复失败');
        }
    }

    /**
     * 更新未读信息
     *
     * @verify int $user_id
     * @verify int $circle_photo_id
     * @return string
     */
    public function updateUnreadAction($user_id, $circle_photo_id)
    {
        // 更新消息为已读
        $unreadCirclePhotoReplyModel = new UnreadCirclePhotoReplyModel();
        $unreadCirclePhotoReplyModel->addWhere('num', '0', '>');
        $unreadCirclePhotoReplyModel->update(
            [
                'num'     => 0,
                'updated' => now(),
            ],
            [
                'user_id'         => $user_id,
                'target_user_id'  => $this->userId,
                'circle_photo_id' => $circle_photo_id,
            ]);

        return success();
    }

    /**
     * 获取未读回复
     *
     * @verify int|null $page
     * @verify int|null $limit
     * @return string
     */
    public function getUnreadAction($page = 1, $limit = 20)
    {
        $unreadCirclePhotoReplyModel = new UnreadCirclePhotoReplyModel();
        $unreadCirclePhotoReplyModel->setFields('user.id as user_id,user.username,user.avatar_url,unread_circle_photo_reply.num,unread_circle_photo_reply.id,circle_photo_reply.content,circle_photo_reply.created,circle_photo_reply.id as circle_photo_reply_id,unread_circle_photo_reply.circle_photo_id');
        $unreadCirclePhotoReplyModel->addWhere([
            'reply.target_user_id' => $this->userId,
        ]);

        $unreadCirclePhotoReplyModel->setJoin('circle_photo_reply', 'circle_photo_reply.id=unread_circle_photo_reply.last_circle_photo_reply_id');
        $unreadCirclePhotoReplyModel->setJoin('user', 'user.id=circle_photo_reply.user_id');
        $unreadCirclePhotoReplyModel->addOrder('circle_photo_reply.created', false);
        $unreadCirclePhotoReplyModel->setPage($page, $limit);
        $data = $unreadCirclePhotoReplyModel->select();

        return success($data);
    }
}